package com.ithuameng.admin.app.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ithuameng.admin.app.system.model.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * User mapper
 *
 * @author ithuameng
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 获取选择的分配分页列表
     *
     * @param page     分页条件
     * @param username
     * @param mobile
     * @param roleId
     * @return
     */
    @Select({
            "<script>",
            "SELECT u.id, u.username, u.password, u.nickname, u.mobile, u.remark, u.status, u.create_time, u.create_by, u.update_time, u.update_by from t_user u",
            "LEFT JOIN t_user_role ur on u.id = ur.user_id",
            "LEFT JOIN t_role r on (r.id = ur.role_id and r.deleted='N')",
            "WHERE u.deleted='N' and r.id = #{roleId}",
            "<if test=\"username != null and username !=''\">and u.username LIKE CONCAT('%',#{username},'%') </if>",
            "<if test=\"mobile != null and mobile !=''\">and u.mobile LIKE CONCAT('%',#{mobile},'%') </if>",
            "</script>"
    })
    IPage<User> selectAllocatedList(IPage<User> page, @Param("username") String username, @Param("mobile") String mobile, @Param("roleId") Long roleId);

    /**
     * 获取未分配分页列表
     *
     * @param page     分页条件
     * @param username
     * @param mobile
     * @param roleId
     * @return
     */
    @Select({
            "<script>",
            "SELECT u.id, u.username, u.password, u.nickname, u.mobile, u.remark, u.status, u.create_time, u.create_by, u.update_time, u.update_by from t_user u",
            "LEFT JOIN t_user_role ur on u.id = ur.user_id", "LEFT JOIN t_role r on(r.id = ur.role_id and  r.deleted='N')",
            "WHERE u.deleted='N' and (r.id != #{roleId} or r.id IS NULL) and u.id not in (select u2.id from t_user u2 inner join t_user_role ur2 on u2.id = ur2.user_id and ur2.role_id = #{roleId} where u2.deleted='N')",
            "<if test=\"username != null and username !=''\">and u.username LIKE CONCAT('%',#{username},'%') </if>",
            "<if test=\"mobile != null and mobile !=''\">and u.mobile LIKE CONCAT('%',#{mobile},'%') </if>",
            "</script>"
    })
    IPage<User> selectUnallocatedList(IPage<User> page, @Param("username") String username, @Param("mobile") String mobile, @Param("roleId") Long roleId);
}
